package com.jeesite.modules.fl.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.fl.entity.Clinic;
import com.jeesite.modules.fl.entity.ClinicUser;
import com.jeesite.modules.fl.service.ClinicService;
import com.jeesite.modules.fl.service.ClinicUserService;
import com.jeesite.modules.sys.entity.DictData;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.UserService;
import com.jeesite.modules.sys.utils.UserUtils;

@Controller
@RequestMapping(value = "${adminPath}/fl/clinicuser")
public class ClinicUserController extends BaseController{
	
	@Autowired
	private ClinicUserService clinicUserService;
	@Autowired
	private ClinicService clinicService;
	@Autowired
	private UserService userService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ClinicUser get(String id, boolean isNewRecord) {
		return clinicUserService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("fl:clinicuser:view")
	@RequestMapping(value = {"list", ""})
	public String list(ClinicUser clinicUser, HttpServletRequest request, HttpServletResponse response, Model model) {
		model.addAttribute("clinicUser", clinicUser);
		Clinic entity = new Clinic();
        entity.setStatus("0");
        User user = UserUtils.getUser();
        if(!user.isAdmin()){
        	entity.getSqlMap().put("dsf", " and (EXISTS (SELECT 1 FROM fl_clinic_user WHERE users like '%"+user.getId()+"%' AND clinic=a.id) or EXISTS (SELECT 1 FROM js_sys_user WHERE user_code='"+user.getId()+"' AND user_code=a.create_by))");
		}
        List<Clinic> clinicList = clinicService.findList(entity);
        model.addAttribute("clinicList", clinicList);
        List<DictData> dicts = new ArrayList<DictData>();
        user = new User();
		user.setStatus("0");
		user.setUserType("employee");
		List<User> users = userService.findList(user);
        for(User u:users){
        	DictData data = new DictData();
        	data.setDictLabelOrig(u.getUserName());
        	data.setDictValue(u.getId());
        	dicts.add(data);
        }
		model.addAttribute("users", dicts);
		return "modules/fl/clinicUserList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("fl:clinicuser:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<ClinicUser> listData(ClinicUser clinicUser, HttpServletRequest request, HttpServletResponse response) {
		User user = UserUtils.getUser();
		if(!user.isAdmin()){
			clinicUser.getSqlMap().put("dsf", " and (EXISTS (SELECT 1 FROM fl_clinic_user WHERE users like '%"+user.getId()+"%' AND clinic=c.id) or EXISTS (SELECT 1 FROM js_sys_user WHERE user_code='"+user.getId()+"' AND user_code=c.create_by))");
		}
		Page<ClinicUser> page = clinicUserService.findPage(new Page<ClinicUser>(request, response), clinicUser);
		return page;
	}
	
	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("fl:clinicuser:view")
	@RequestMapping(value = "form")
	public String form(String clinicId, String id, Model model) {
		ClinicUser clinicUser = null;
		if(id != null && !id.isEmpty() && !id.equals("undefined")){
			clinicUser = get(id, false);
		}else{
			clinicUser = new ClinicUser();
			clinicUser.setClinic(clinicService.get(clinicId));
		}
		User user = new User();
		user.setStatus("0");
		user.setUserType("employee");
		List<User> users = userService.findList(user);
		model.addAttribute("clinicUser", clinicUser);
		model.addAttribute("users", users);
		return "modules/fl/clinicUserForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("fl:clinicuser:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated ClinicUser clinicUser) {
		clinicUserService.save(clinicUser);
		return renderResult(Global.TRUE, text("保存数据成功！"));
	}

}
